//
//  Forecast.h
//  iWeather
//
//  Created by Vo Thi My Hanh (FSU1.BU19) on 9/18/13.
//  Copyright (c) 2013 Vo Thi My Hanh (FSU1.BU19). All rights reserved.
//

#import "DownloadImageManager.h"
#import "WeatherDefines.h"


@implementation DownloadImageManager

static DownloadImageManager *shared;
+(DownloadImageManager*)Shared
{
    if(!shared)
        shared = [[DownloadImageManager alloc] init];
    return shared;
}

-(void)clearCache
{
    removePath(CACHE_IMAGE_DIR);
}

-(void)clearsubDirectory:(NSString *)subdirectory
{
    
    if(subdirectory != nil && ![subdirectory isEqualToString:@""]){
        NSString *str = [NSString stringWithFormat:@"%@%@/",CACHE_IMAGE_DIR, subdirectory];
        int requestTag = [subdirectory intValue];
        if(requestTag > 0){
            for(ASIHTTPRequest *request_ in requests){
                if(request_.tag == requestTag){
                    
                    [request_ setDownloadDestinationPath:@""];
                    [request_ clearDelegatesAndCancel];
                     
  
                }
            }
        }
        removePath(str);
    }
}

-(void)clearAllCache
{
    removePath(CACHE_IMAGE_DIR);
}



-(id)init
{
    self = [super init];
    requests = [[NSMutableArray alloc] init];
    return self;
}

-(BOOL)downloadWithObject:(ASIHTTPRequest*)request
{
    //check if file exists


    [requests addObject:request];
    return YES;
}

-(void)cancelAllDownloads
{
    for(ASIHTTPRequest *request_ in requests){
        [request_ clearDelegatesAndCancel];
    }
    [requests removeAllObjects];
}
@end
